package xyz.jpcr.covid19app.Fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.eazegraph.lib.charts.PieChart;
import org.eazegraph.lib.models.PieModel;

import java.text.NumberFormat;
import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import xyz.jpcr.covid19app.Activity.CountryWiseActivity;
import xyz.jpcr.covid19app.Api.OverallApiClient;
import xyz.jpcr.covid19app.Model.World;
import xyz.jpcr.covid19app.R;
import xyz.jpcr.covid19app.Util.IDateUtil;

public class OverallFragment extends Fragment {

    
    ProgressDialog progressDialog;
    Context context;
    TextView fragment_overall_CurrentConfirmed, fragment_overall_confirmed, fragment_overall_suspected, fragment_overall_deaths, fragment_overall_Cured, fragment_overall_clock, fragment_overall_day, fragment_overall_tests;
    PieChart fragment_overall_piechart;
    SwipeRefreshLayout swipeRefreshLayout;
    boolean isrefresh = false;
    LinearLayout otherCountry_btn;

    
    public OverallFragment() {
    }
    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.fragment_overall, container, false);
        context = getContext();
//        ((AppCompatActivity) requireActivity()).getSupportActionBar().setTitle(context.getText(R.string.covid_19_tracking) + "(Overall)");
        fragment_overall_CurrentConfirmed = view.findViewById(R.id.fragment_overall_CurrentConfirmed);
        fragment_overall_confirmed = view.findViewById(R.id.fragment_overall_confirmed);
        fragment_overall_suspected = view.findViewById(R.id.fragment_overall_suspected);
        fragment_overall_deaths = view.findViewById(R.id.fragment_overall_deaths);
        fragment_overall_Cured = view.findViewById(R.id.fragment_overall_Cured);
        fragment_overall_clock = view.findViewById(R.id.fragment_overall_clock);
        fragment_overall_day = view.findViewById(R.id.fragment_overall_day);
        fragment_overall_piechart = view.findViewById(R.id.fragment_overall_piechart);
        otherCountry_btn = view.findViewById(R.id.fragment_other_country_btn);
        fragment_overall_tests = view.findViewById(R.id.fragment_overall_tests);

        swipeRefreshLayout = view.findViewById(R.id.fragment_overall_swipefreshlayout);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isrefresh = true;
                getJson();
            }
        });

        otherCountry_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), CountryWiseActivity.class);
                startActivity(intent);
            }
        });

        showProgressDialog();
        getJson();

        return view;
    }


    public void showProgressDialog() {
        progressDialog = new ProgressDialog(context);
        progressDialog.show();
        progressDialog.setContentView(R.layout.progress_dialog);
        progressDialog.setCanceledOnTouchOutside(false);
        Objects.requireNonNull(progressDialog.getWindow()).setBackgroundDrawableResource(android.R.color.transparent); // 背景透明化
    }


    void getJson() {
        Call<World> call =  OverallApiClient.getInstance().getApi().getWorldData();
        call.enqueue(new Callback<World>() {
            @Override
            public void onResponse(Call<World> call, Response<World> response) {
                if(response.code() == 200 && response.body() != null) {
                    World res = response.body();
                    fragment_overall_CurrentConfirmed.setText(NumberFormat.getInstance().format(res.getActive()));
                    fragment_overall_confirmed.setText(NumberFormat.getInstance().format(res.getCases()));
                    fragment_overall_suspected.setText(NumberFormat.getInstance().format(res.getCritical()));
                    fragment_overall_deaths.setText(NumberFormat.getInstance().format(res.getDeaths()));
                    fragment_overall_Cured.setText(NumberFormat.getInstance().format(res.getRecovered()));
                    fragment_overall_clock.setText(IDateUtil.getInstance().formatDate(res.getUpdated(), 2));
                    fragment_overall_day.setText(IDateUtil.getInstance().formatDate(res.getUpdated(), 1));
                    fragment_overall_tests.setText(NumberFormat.getInstance().format(res.getTests()));

                    fragment_overall_piechart.clearChart();
                    fragment_overall_piechart.addPieSlice(new PieModel("Current Confirmed", res.getActive(), Color.parseColor("#fbc233")));
                    fragment_overall_piechart.addPieSlice(new PieModel("Suspected", res.getCritical(), Color.parseColor("#08a045")));
                    fragment_overall_piechart.addPieSlice(new PieModel("Deaths", res.getDeaths(), Color.parseColor("#f6404f")));
                    fragment_overall_piechart.addPieSlice(new PieModel("Cured", res.getRecovered(), Color.parseColor("#FF03DAC5")));
                    if (isrefresh) {
                        Toast.makeText(context, context.getText(R.string.data_refresh), Toast.LENGTH_SHORT).show();
                    }
                    fragment_overall_piechart.startAnimation();
                }
                else {
                    Toast.makeText(context, context.getText(R.string.network_slow), Toast.LENGTH_SHORT).show();
                }
                if(isrefresh) {
                    isrefresh = false;
                    swipeRefreshLayout.setRefreshing(false);
                }
                progressDialog.cancel();
            }

            @Override
            public void onFailure(Call<World> call, Throwable t) {
                if(isrefresh) {
                    isrefresh = false;
                    swipeRefreshLayout.setRefreshing(false);
                }
                Toast.makeText(context, context.getText(R.string.network_slow), Toast.LENGTH_SHORT).show();
            }
        });
    }
}